<?php
class Cat_model extends MY_Model {

    function __construct(){
        parent::__construct();
    }

    function get_all(){
    	
    	$query = $this->db->from($this->tbl->cat.' c')
    		->select('c.*,count(t.id) as count_topic')
    		->join($this->tbl->topic.' t','t.cat_id=c.id and t.pid=0 and t.status=1','left')
    		->group_by('c.id')
    		->get();
    	$list = $query->result();
    	
    	foreach ($list as $k=>$v){
    		
    		$list_arr = json_decode(json_encode($list),true);
    		$arr_sub = getCatSubId($list_arr, $v->id);
    		$count = 0;
    		foreach ($list as $kk=>$vv){
    			if(in_array($vv->id, $arr_sub)) $count += $vv->count_topic;
    		}
    		
    		$list[$k]->count_topic = $count;
    	}
    	
    	return $list;
    }
    
}